import {
  modelOptions,
  Prop,
  plugin,
  index,
  getModelForClass,
  ReturnModelType
} from '@typegoose/typegoose';
import { AutoIncrementID } from '@typegoose/auto-increment';
import { ApiProperty, OmitType, PartialType, PickType } from '@nestjs/swagger';

// 使用AutoIncrementID插件，设置userId字段自增，从1开始
@plugin(AutoIncrementID, { field: 'roleId', startAt: 1 })
// 创建索引，username字段唯一
@index({ name: 1 }, { unique: true })
@modelOptions({
  schemaOptions: {
    //数据库集合的名称
    collection: 'roles',
    //数据库创建及修改时间自动完善填充
    timestamps: true
  }
})
export class Role {
  @Prop()
  readonly roleId: number;

  @ApiProperty({ description: '角色名称', type: 'string' })
  @Prop({ required: true, trim: true, unique: true })
  name: string;

  @ApiProperty({ description: '角色描述', type: 'string', required: false })
  @Prop()
  intro?: string;

  @ApiProperty({ description: '菜单列表', type: [String], required: false })
  @Prop({ type: [String] })
  menuIds?: string[];
}

// 导出UserModel，用于获取User模型
export const RoleModel = getModelForClass(Role);

// 定义UserModelType，用于返回User模型的类型
export type RoleModelReturnType = ReturnModelType<typeof Role>;

export class CreateRoleDto extends OmitType(Role, ['roleId'] as const) {}
export class UpdateRoleDto extends PartialType(CreateRoleDto) {}
